package org.csu.goodpartner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.goodpartner.VO.ItemVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Inventory;
import org.csu.goodpartner.entity.Item;
import org.csu.goodpartner.persistence.InventoryMapper;
import org.csu.goodpartner.persistence.ItemMapper;
import org.csu.goodpartner.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public CommonResponse publish(ItemVO itemVO) {
        Item item = new Item();
        item.setCategory(itemVO.getCategory());
        item.setClassone(itemVO.getClassone());
        item.setClasstwo(itemVO.getClasstwo());
        item.setImg(itemVO.getImg());
        item.setDescription(itemVO.getDescription());
        item.setListprice(itemVO.getListprice());
        item.setName(itemVO.getName());
        item.setShopid(itemVO.getShopid());
        item.setStatus(1);
        int m = itemMapper.insert(item);
        System.out.println(m);

        Integer itemId = item.getItemid();

        Inventory inventory = new Inventory();
        inventory.setItemid(itemId);
        inventory.setShopid(itemVO.getShopid());
        inventory.setQuantity(itemVO.getQuantity());
        int n = inventoryMapper.insert(inventory);
        System.out.println(n);

        if(m > 0 && n > 0)
            return CommonResponse.createForSuccessMessage("开设店铺成功！");
        else
            return CommonResponse.createForError("开设店铺失败！");
    }

    @Override
    public CommonResponse update(ItemVO itemVO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        System.out.println(itemVO.getItemid()+"!!!!!!!!!!!!!!!!!!!!");
        queryWrapper.eq("itemid",itemVO.getItemid());
        Item item = itemMapper.selectOne(queryWrapper);
        System.out.println(item.getItemid()+"================================");
        //item.setCategory(itemVO.getCategory());
        item.setClassone(itemVO.getClassone());
        item.setClasstwo(itemVO.getClasstwo());
        //item.setImg(itemVO.getImg());
        //item.setDescription(itemVO.getDescription());
        item.setListprice(itemVO.getListprice());
        item.setName(itemVO.getName());
        item.setShopid(itemVO.getShopid());
        //item.setStatus(1);
        int m = itemMapper.update(item,queryWrapper);
        System.out.println(m);

        //queryWrapper.eq("shopid",itemVO.getShopid());
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        inventory.setItemid(itemVO.getItemid());
        inventory.setShopid(itemVO.getShopid());
        inventory.setQuantity(itemVO.getQuantity());
        int n = inventoryMapper.update(inventory,queryWrapper);
        System.out.println(n);

        if(m > 0 || n > 0)
            return CommonResponse.createForSuccessMessage("修改成功！");
        else
            return CommonResponse.createForError("请输入新的信息！");
    }

    @Override
    public CommonResponse out(int shopId, int itemId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemid",itemId);
        Item item = itemMapper.selectOne(queryWrapper);
        item.setStatus(0);
        int m = itemMapper.update(item,queryWrapper);
        System.out.println(m);

        if(m > 0)
            return CommonResponse.createForSuccessMessage("商品已经下架！");
        else
            return CommonResponse.createForError("操作错误，请重试！");
    }

    @Override
    public CommonResponse getAllItems(int shopId) {
        List<ItemVO> itemVOList = itemMapper.getAllItems(shopId);
        return CommonResponse.createForSuccess(itemVOList);
    }

    @Override
    public CommonResponse like(int shopId, String like) {
        List<ItemVO> itemList=itemMapper.searchAll(like, shopId);
        return CommonResponse.createForSuccess("展示所有商品",itemList);
    }

    /*
    @Override
    public CommonResponse like(int shopId, String like) {
        QueryWrapper<ItemVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shopid",shopId);
        queryWrapper.and(
                wrapper->wrapper.like("itemid", Integer.valueOf(like))
                        .or()
                        .like("category",like)
                        .or()
                        .like("name",like)
                        .or()
                        .like("description",like)
                        .or()
                        .like("shopid",Integer.valueOf(like))
                        .or()
                        .like("status",Integer.valueOf(like))
                        .or()
                        .like("classone",like)
                        .or()
                        .like("classtwo",like)
                        .or()
                        .like("img",like)
                        .or()
                        .like("listprice",Integer.valueOf(like))
                        .or()
                        .like("quantity",Integer.valueOf(like))
        );
        List<Item> itemList = itemMapper.selectList(queryWrapper);

        return CommonResponse.createForSuccess(itemVOList);
    }
    */
}
